from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login, logout
from django.http import Http404, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt
from blog.models import Article
from django.contrib.auth.models import User
import os


# Create your views here.

def index(request):
    if request.user.is_authenticated():
        return render_to_response('index.html', {'user': request.user})
    else:
        return render_to_response('index.html')

@csrf_exempt
def login_view(request):
    user = authenticate(username=request.POST['username'], password=request.POST['password'])
    if user is not None:
        if user.is_active:
            login(request, user)
            return HttpResponseRedirect('/', {'user': request.user})
        else:
            return render_to_response('accounts/error.html')
    else:
        return render_to_response('accounts/error.html')

def logout_view(request):
    logout(request)
    return HttpResponseRedirect('/')

def register(request):
    return render_to_response('accounts/register.html')

@csrf_exempt
def register_go(request):
    username = request.POST['username']
    password = request.POST['password']
    email = request.POST['email']
    user = User.objects.create_user(username, password, email)
    if user is not None:
        user.save()
        return render_to_response('accounts/success.html')
    else:
        return render_to_response('accounts/error.html')

def product_list(request):
    if request.user.is_authenticated():
        return render_to_response('product_list.html', {'user': request.user})
    else:
        return render_to_response('accounts/login_remaind.html')

def netcard_info(request):
    if request.user.is_authenticated():
        return render_to_response('netcard_info.html', {'user': request.user})
    else:
        return render_to_response('accounts/login_remaind.html')

def netcard_info_go(request):
    user = request.user
    netcard = request.GET['netcard']
    bak = os.popen('ifconfig %s'%netcard).read().split('\n')
    if '' in bak:
        return render_to_response('netcard_info.html', locals())
    else:
        return render_to_response('netcard_info.html')

def ping_test(request):
    if request.user.is_authenticated():
        return render_to_response('ping_test.html', {'user': request.user})
    else:
        return render_to_response('accounts/login_remaind.html')

def ping_test_go(request):
    user = request.user
    ipaddr = request.GET['ipaddr']
    bak = os.popen('ping %s -c 4'%ipaddr).read().split('\n')
    if '' in bak:
        return render_to_response('ping_test.html', locals())
    else:
        return render_to_response('ping_test.html')

def sys_info(request):
    user = request.user
    bak = os.popen("cat /proc/cpuinfo |grep model\ name | awk -F ':' '{print $2;exit}'").read()
    MemTotal = int(os.popen("cat /proc/meminfo |grep MemTotal | awk '{print $2;exit}'").read())
    MemFree = int(os.popen("cat /proc/meminfo |grep MemFree | awk '{print $2;exit}'").read())
    Mem = (MemTotal-MemFree)*100/MemTotal
    return render_to_response('sys_info.html', locals())


def myblog(request):
    if request.user.is_authenticated():
        user = request.user
        db = Article.objects.filter(username=user)
        return render_to_response('myblog/myblog.html',locals())
    else:
        return render_to_response("accounts/login_remaind.html")

def myblog_list(request):
    if request.user.is_authenticated():
        user = request.user
        db = Article.objects.filter(username=user)
        return render_to_response('myblog/myblog_list.html',locals())
    else:
        return render_to_response("accounts/login_remaind.html")

@csrf_exempt
def myblog_create(request):
    if request.user.is_authenticated():
        user = request.user
        db = Article.objects.filter(username=user)
        return render_to_response('myblog/myblog_create.html',locals())
    else:
        return render_to_response("accounts/login_remaind.html")